//
//  BHCircleRelationNavigationBar.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/7/14.
//

import UIKit

class BHCircleRelationNavigationBar: UIView {

    var didChangeIndexActBlock:((Int) -> Void)?
    var goSearchBlock:((String) -> Void)?

    
    @IBOutlet weak var search_textFiled: UITextField!
    @IBOutlet weak var back_btn: UIButton!
    @IBOutlet weak var sportBtn: UIButton!
    
    @IBOutlet weak var gameBtn: UIButton!
    
    @IBOutlet weak var funBtn: UIButton!
    
    fileprivate var remarkBtn: UIButton!
    
    @IBOutlet weak var searchBackView: UIView!
    @IBOutlet weak var begain_state: NSLayoutConstraint!
    
    
    static func loadFromXib() -> BHCircleRelationNavigationBar {
        let viewArray = Bundle.main.loadNibNamed("BHCircleRelationNavigationBar", owner: nil, options: nil)
        let view: BHCircleRelationNavigationBar = viewArray![0] as! BHCircleRelationNavigationBar
        return view
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.search_textFiled.delegate = self

        self.begain_state.constant = UIScreen.stateBarHeight
        self.remarkBtn = self.sportBtn
        
        self.searchBackView.addLayerCorner(15, 0.5, .color_cccccc)
        
        
    }
    
    
    @IBAction func changeIndexAct(_ sender: UIButton) {
        if sender.isSelected == true {
            return
        }
        
        sender.isSelected = true
        self.remarkBtn.isSelected = false
        self.remarkBtn = sender
        
        self.didChangeIndexActBlock?(sender.tag-100)
        
    }
    
    static var secHeight: CGFloat {
        get {
            return UIScreen.begainY+50
        }
    }

    
}

extension BHCircleRelationNavigationBar : UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        self.goSearchBlock?(textField.text ?? "")
    }
}



class BHCircleChoseAreaNavigationBar: UIView {
    @IBOutlet weak var searchBackView: UIButton!
    @IBOutlet weak var begain_state: NSLayoutConstraint!
    @IBOutlet weak var back_btn: UIButton!

    @IBOutlet weak var confirmBtn: UIButton!
    
    static func loadFromXib() -> BHCircleChoseAreaNavigationBar {
        let viewArray = Bundle.main.loadNibNamed("BHCircleRelationNavigationBar", owner: nil, options: nil)
        let view: BHCircleChoseAreaNavigationBar = viewArray![1] as! BHCircleChoseAreaNavigationBar
        return view
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        

        self.begain_state.constant = UIScreen.stateBarHeight
        
        self.searchBackView.addLayerCorner(15, 0.5, .color_cccccc)
        self.confirmBtn.addLayerCorner(12, 0, .color_cccccc)

        
    }
    
    
    static var secHeight: CGFloat {
        get {
            return UIScreen.begainY+52
        }
    }
}



protocol BHCircleSearchAreaNavigationBarDelegate {
    func clearSearchText()
    func goSearchWithKeyword(text: String)
}
class BHCircleSearchAreaNavigationBar: UIView {
    
    var delegate: BHCircleSearchAreaNavigationBarDelegate?
    
    
    @IBOutlet weak var searchBackView: UIView!
    
    @IBOutlet weak var cancelBtn: UIButton!
    
    @IBOutlet weak var searchTextFiled: UITextField!
    
    static func loadFromXib() -> BHCircleSearchAreaNavigationBar {
        let viewArray = Bundle.main.loadNibNamed("BHCircleRelationNavigationBar", owner: nil, options: nil)
        let view: BHCircleSearchAreaNavigationBar = viewArray![2] as! BHCircleSearchAreaNavigationBar
        return view
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        self.searchTextFiled.delegate = self
        self.searchBackView.addLayerCorner(16, 0, .color_cccccc)
        
    }
    
    @IBAction func clearAct(_ sender: UIButton) {
        self.searchTextFiled.text = ""
        self.delegate?.clearSearchText()
    }
    
    static var secHeight: CGFloat {
        get {
            return UIScreen.stateBarHeight+52
        }
    }
}


extension BHCircleSearchAreaNavigationBar: UITextFieldDelegate {
    func textFieldDidEndEditing(_ textField: UITextField) {
        self.delegate?.goSearchWithKeyword(text: textField.text ?? "")
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
}
